import turtle

def sierpinski(degree, points):
    colormap = ['blue', 'red', 'green', 'white', 'yellow', 'orange']
    drawTriangle(points, colormap[degree])

    if degree > 0:
        sierpinski(degree-1,
            {'left':points['left'],
            'top': getMid(points['left'], points['top']),
            'right': getMid(points['left'], points['right'])})
        sierpinski(degree-1,
            {'left': getMid(points['left'], points['top']),
            'top': points['top'],
            'right':getMid(points['right'],points['top'])})
        sierpinski(degree-1,
            {'left': getMid(points['left'], points['right']),
            'top': getMid(points['right'],points['top']),
            'right':points['right']})



def getMid(p1,p2):
    return ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2)

def drawTriangle(points, color):
    t.fillcolor(color)
    t.penup()
    t.goto(points['top'])
    t.pendown()
    t.begin_fill()
    t.goto(points['left'])
    t.goto(points['right'])
    t.goto(points['top'])
    t.end_fill()


t = turtle.Turtle()
points={'left':(-200,-100), "right":(200, -100), 'top':(0, 200)}

sierpinski(5, points)

turtle.done()